package com.gugee.ins.data.blogger.es.core;

import com.gugee.ins.common.dal.service.media.InsMediaDao;
import com.gugee.ins.common.dal.service.media.InsRelMediaTaggerUserDao;
import com.gugee.ins.common.model.es.blogger.InsBlogger;
import com.gugee.ins.common.model.mysql.blogger.InsBloggerMain;
import com.gugee.ins.data.blogger.es.config.InsBloggerEsMapping;
import com.xiguaji.boot.toolkit.ThreadPoolUtils;
import com.xiguaji.boot.toolkit.elasticsearch.RestClientHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

@Slf4j
@Service
public class BloggerMainUpdateEsService extends EsBaseService{

    String threadPoolName = "BloggerMainUpdateEs-Thread";

    @Autowired
    InsMediaDao insMediaDao;

    @Autowired
    InsRelMediaTaggerUserDao insRelMediaTaggerUserDao;

    @Autowired
    InsBloggerEsMapping insBloggerEsMapping;

    @PostConstruct
    public void init() {
        forkJoinPool = ThreadPoolUtils.forkJoinPool(bloggerToElasticsearchAppConfig.getBloggerMainUpdateEsThreadSize(), threadPoolName);
        scheduleTemplate.attach(threadPoolName, forkJoinPool);
        workName = "BloggerMainUpdateEs";
    }

    @Override
    protected InsBlogger structureEsBlogger(InsBloggerMain insBloggerMain) {
        InsBlogger insBloggerEs = insBloggerEsMapping.mapper(insBloggerMain);
        //标记博主
        insBloggerEs.setTaggedBrand(insRelMediaTaggerUserDao.getTaggedBrandByUid(insBloggerMain.getUid()));
        return insBloggerEs;
    }

    @Override
    protected void toEsHandler(String esIndex, List<InsBlogger> insBloggers) {
        RestClientHelper.upsert(esIndex,insBloggers);
    }
}
